package com.example.myfoodrecipe.fragments.recipe

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myfoodrecipe.databinding.FragmentRecipeBinding
import com.example.myfoodrecipe.fragments.recipe.adapter.FoodAdapter
import com.example.myfoodrecipe.fragments.recipe.adapter.TypeAdapter
import com.example.myfoodrecipe.util.NetworkResult
import com.example.myfoodrecipe.util.showToast
import com.example.myfoodrecipe.viewModel.MainViewModel


class RecipeFragment : Fragment() {
    private lateinit var binding: FragmentRecipeBinding
    private val typeAdapter = TypeAdapter()
    private val foodAdapter = FoodAdapter()

    private val mainViewModel:MainViewModel by viewModels()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentRecipeBinding.inflate(inflater)
        //init recyclerView
        initRecyclerView()

        mainViewModel.recipes.observe(viewLifecycleOwner){
            when(it){
                //成功
                is NetworkResult.Success -> {
                    binding.foodRecyclerView.hideShimmer()
                    foodAdapter.setData(it.data!!.results)
                }
                //加载中
                is NetworkResult.Loading -> {
                    binding.foodRecyclerView.showShimmer()
                }
                //失败
                is NetworkResult.Error -> {
                    binding.foodRecyclerView.hideShimmer()
                    showToast(requireContext(),it.message!!)
                }


            }

        }
        fetchData("main course")
        return binding.root
    }



    private fun initRecyclerView() {
        //init type_recyclerView
        binding.typeRecyclerView.layoutManager = LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
        binding.typeRecyclerView.adapter = typeAdapter

        //init food_recyclerview
        binding.foodRecyclerView.layoutManager = GridLayoutManager(requireContext(),2)
        binding.foodRecyclerView.adapter = foodAdapter


        //处理回调来改变点击后上一个item的选择状态
        typeAdapter.callBack = {current, last ->
            if (current != last){
                val curHolder: TypeAdapter.ItemViewHolder = binding.typeRecyclerView.findViewHolderForAdapterPosition(current)
                        as TypeAdapter.ItemViewHolder
                curHolder.changeSelectedState(true)

                //lastHolder有可能为空（当前positionItem被滑到了屏幕外）
                val lastHolder = binding.typeRecyclerView.findViewHolderForAdapterPosition(last)
                if (lastHolder != null){
                    lastHolder as TypeAdapter.ItemViewHolder
                    lastHolder.changeSelectedState(false)
                }else{
                    //刷新item
                    typeAdapter.notifyItemChanged(last)
                }
            }

            //获取数据
            fetchData(typeAdapter.typeList[current])
        }


    }

    private fun fetchData(type:String) {
        mainViewModel.fetchFoodRecipe(type)
    }

}